require "game.cl_init"

module("GameSound", package.seeall )

DEBUG = false

DROP = 1
HAS = 2
RETURN = 3
SCORE = 4
WIN = 5

SoundTeamList = {
	[Team.RED] = {
		[DROP] = Sound("ctf/reddrop.wav"),
		[HAS] = Sound("ctf/redhas.wav"),
		[RETURN] = Sound("ctf/redreturn.wav"),
		[SCORE] = Sound("ctf/redscore.wav"),
		[WIN] = Sound("ctf/redwin.wav")
	},
	[Team.BLUE] = {
		[DROP] = Sound("ctf/bluedrop.wav"),
		[HAS] = Sound("ctf/bluehas.wav"),
		[RETURN] = Sound("ctf/bluereturn.wav"),
		[SCORE] = Sound("ctf/bluescore.wav"),
		[WIN] = Sound("ctf/bluewin.wav")
	},
}

local Queue = {}
local NextQueueThink = 0.0

function AddToQueue( sound )

	if #Queue > 3 then
		Queue = {}
	end
	
	table.insert( Queue, sound )
	
end

hook.Add("Think", "SoundQueueThink", function()
	
	if CurTime() > NextQueueThink && #Queue > 0 then
		
		local SoundPath = table.remove( Queue )
		local Duration = SoundDuration( SoundPath )
		
		surface.PlaySound( SoundPath )	
		
		if DEBUG then
			print("Playing sound: " .. SoundPath .. "(".. Duration ..")")
		end
		
		NextQueueThink = CurTime() + Duration
	
	end
	
	
end )

function PlayTeam( team, sound )
	
	local List = SoundTeamList[ team ]
	
	if List && List[ sound ] then
		AddToQueue( List[sound] )
	end
	
end

hook.Add("StateChange", "PlayStateSound", function( state )
	
	if state == Game.BUILD then
		surface.PlaySound("ctf/Build.wav")
	elseif state == Game.FIGHT then
		surface.PlaySound("ctf/fight.wav")
	end
	
end )